<?php 
namespace app\index\controller;
use think\Controller;
use think\Log;
use think\Request;
use think\Exception;
use think\exception\HttpException;
use think\Debug;
use think\Db;
use app\index\model\Notice as NoticeModel;

class Logs extends Controller {

    public function index() {
        Log::init([
            'type' => 'File',
            'path' => APP_PATH.'logs/',
            // 只记录此级别的日志
            // 'level' => ['error'],
            'apart_level' => ['error']
        ]);

        Log::record('测试日志信息', 'error'); // 写入内存
        // Log::save(); // 从内存中写到指定日志中
        // Log::write('测试日志信息', 'error'); // 写入日志信息

        Log::clear(); // 晴空内存中的日志

    }

    public function allow() {
        Log::init([
            'type' => 'File',
            'path' => APP_PATH.'logs/',
            'allow_key' => ['127.0.0.2'] // 只允许这个ip纪录日志
        ]);

        // 指定key作为可纪录日志
        Log::key(Request::instance()->ip());
    }

    public function debug() {

        Debug::remark('begin');
        $a = 1;
        while($a++) {
            if($a > 100) {
                $a = 0;
            }
        }
        // Debug::remark('end');
        echo Debug::getRangeTime('begin','end').'s';
        echo Debug::getRangeMem('begin','end');

        Db::listen(function($sql, $time, $explain) {
            var_dump($sql.'['.$time.'s]');
            var_dump($explain);
        });

        NoticeModel::get(1);
        var_dump(NoticeModel::getLastSql());

        // throw new Exception('异常信息', 1000);
        throw new HttpException(404, '异常页面');
    }

}